package com.avic.rong.activity.mine;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.activity.product.OnlineCServiceActivity;
import com.avic.rong.bean.CartListReloadEvent;
import com.avic.rong.bean.GoCartListEvent;
import com.avic.rong.bean.HomePageEvent;
import com.avic.rong.bean.MinePageEvent;
import com.avic.rong.fragment.balance.AccountBalanceFragment;
import com.avic.rong.fragment.balance.BzMemberFragment;
import com.avic.rong.fragment.balance.MemberCardFragment;
import com.avic.rong.shoppingCar.activity.ShoppingCarActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

//资金明细-账号余额
public class CapitalSubsidiaryActivity extends BaseActivity {

    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.fram_capital)
    FrameLayout fram_capital;
    @BindView(R.id.ima_return)
    ImageView ima_return;
    @BindView(R.id.img_function)
    ImageView img_function;
    private FragmentTransaction fragmentTransaction;
    private AccountBalanceFragment accountBalanceFragment;
    private MemberCardFragment memberCardFragment;
    private PopupWindow popupWindow;
    private View popView;
    private BzMemberFragment bzMemberFragment;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_capital_subsidiary;
    }

    @Override
    protected void initView() {
        isNeedTitle = false;
        boolean memberCard = getIntent().getBooleanExtra("memberCard", false);
        boolean bzMemberCard = getIntent().getBooleanExtra("bzMemberCard", false);
        double balance = getIntent().getDoubleExtra("balance",0);

        fragmentTransaction = getSupportFragmentManager().beginTransaction();

        accountBalanceFragment = new AccountBalanceFragment();
        memberCardFragment = new MemberCardFragment(memberCard,balance);
        bzMemberFragment = new BzMemberFragment(bzMemberCard,balance);

        fragmentTransaction.add(R.id.fram_capital, accountBalanceFragment);
        fragmentTransaction.add(R.id.fram_capital, memberCardFragment);
        fragmentTransaction.add(R.id.fram_capital, bzMemberFragment);

        if (memberCard) {
            tv.setText("融融会员卡");
            fragmentTransaction.show(memberCardFragment).hide(accountBalanceFragment).hide(bzMemberFragment).commit();
        } else {
            if (bzMemberCard){
                tv.setText("融融标准会员卡");
                fragmentTransaction.show(bzMemberFragment).hide(accountBalanceFragment).hide(memberCardFragment).commit();
            }else {
                tv.setText("账号余额");
                //默认显示账户余额
                fragmentTransaction.show(accountBalanceFragment).hide(memberCardFragment).hide(bzMemberFragment).commit();
            }
        }
    }

    @Override
    protected void addListener() {

    }

    @OnClick({ R.id.ima_return, R.id.img_function,R.id.ll_capital})
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.ll_capital:
                showPupopwindow();
                break;
            case R.id.ima_return:
                finish();
                break;
            case R.id.img_function:
                showPopup(CapitalSubsidiaryActivity.this);
                break;
        }
    }

    public void showPopup(BaseActivity baseActivity){
        popView = LayoutInflater.from(baseActivity).inflate(R.layout.function_of_direct, null, false);
        commonPop(popView);
        initListener(baseActivity);
    };
    /*
     * 功能直达蒙层
     * */
    private void commonPop(View view) {
        popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
       /* // 产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);*/
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setClippingEnabled(false);
        //从上往下弹出动画效果
        popupWindow.setAnimationStyle(R.style.topPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.rl_root), Gravity.TOP, 0, 0);
    }
    private void initListener(Context context) {
        ImageView img_close_pop = popView.findViewById(R.id.img_close_pop);
        Button bt_home = popView.findViewById(R.id.bt_home);
        Button bt_mall = popView.findViewById(R.id.bt_mall);
        Button bt_news = popView.findViewById(R.id.bt_news);
        Button bt_own = popView.findViewById(R.id.bt_own);
        Button bt_goods_car = popView.findViewById(R.id.bt_goods_car);
        Button bt_kefu = popView.findViewById(R.id.bt_kefu);

        img_close_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        bt_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventBus.getDefault().postSticky(new HomePageEvent());
                Intent intent = MainActivity.queryIntent(context);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });
        bt_mall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventBus.getDefault().postSticky(new GoCartListEvent());
                Intent intent = MainActivity.queryIntent(context);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });
        bt_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventBus.getDefault().postSticky(new CartListReloadEvent());
                startActivity(MainActivity.queryIntent(context));
            }
        });
        bt_own.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventBus.getDefault().postSticky(new MinePageEvent());
                Intent intent = MainActivity.queryIntent(context);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });
        bt_goods_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                startActivity(ShoppingCarActivity.queryIntent(context));
            }
        });
        bt_kefu.setOnClickListener(new View.OnClickListener() {//客服
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                startActivity(OnlineCServiceActivity.getIntent(context,"123456"));
            }
        });


    }
    private void showPupopwindow() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_capital, null, false);
        inflate.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setClippingEnabled(false);
        popupWindow.setContentView(inflate);
        popupWindow.setFocusable(true);

        int measuredWidth = tv.getMeasuredWidth();
        int measuredWidth1 = popupWindow.getContentView().getMeasuredWidth();
        int xPos = ( measuredWidth - measuredWidth1)/2;

        popupWindow.showAsDropDown(tv, xPos, 0);



        TextView tv_account_balance = inflate.findViewById(R.id.tv_account_balance);
        TextView tv_member_card = inflate.findViewById(R.id.tv_member_card);
        TextView tv_bzmember_card = inflate.findViewById(R.id.tv_bzmember_card);

        String trim = tv.getText().toString().trim();
        String accountBalance = tv_account_balance.getText().toString().trim();
        String memberCard = tv_member_card.getText().toString().trim();
        String bzmemeberCard = tv_bzmember_card.getText().toString().trim();
        if (trim.equals(accountBalance)){
            tv_account_balance.setTextColor(getResources().getColor(R.color.color_2098C727));
        }else if (trim.equals(memberCard)){
            tv_member_card.setTextColor(getResources().getColor(R.color.color_2098C727));
        }else if (trim.equals(bzmemeberCard)){
            tv_bzmember_card.setTextColor(getResources().getColor(R.color.color_2098C727));
        }



        tv_account_balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("账号余额");
                getSupportFragmentManager().beginTransaction().show(accountBalanceFragment).hide(memberCardFragment).hide(bzMemberFragment).commit();
                popupWindow.dismiss();
            }
        });
        tv_member_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("融融会员卡");
                getSupportFragmentManager().beginTransaction().show(memberCardFragment).hide(accountBalanceFragment).hide(bzMemberFragment).commit();
                popupWindow.dismiss();
            }
        });

        tv_bzmember_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("融融标准会员卡");
                getSupportFragmentManager().beginTransaction().show(bzMemberFragment).hide(accountBalanceFragment).hide(memberCardFragment).commit();
                popupWindow.dismiss();
            }
        });
    }


    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, CapitalSubsidiaryActivity.class);
        return intent;
    }
}
